package panes;

import dialogs.DlgAddAction;
import entities.Action;
import entities.Categorie;
import entities.Month;
import entities.Year;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Calendar;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import utils.ChocoBar;
import utils.ChocoMessages;
import utils.ChocoPane;

/**
 *
 * @author chocoseb
 */
public class Resume extends ChocoPane {

    private Year _year;
    private frames.Main _parentFrame;

    /** Creates new form Resume */
    public Resume(frames.Main _parent, ChocoBar _status, Year p_year) {
        super(_status, _parent);
        _year = p_year;
        _parentFrame = _parent;
        initComponents();
        _year.addMonthSelectionComboBox(cmbMonths);
        _year.addNextButton(btnNext);
        _year.addPreviousButton(btnPrevious);
        _year.addActionsTable(tblActions);
        _year.bindDeficit(lblTotalDeficit);
        _year.bindExpenseLabel(lblTotalExpense);
        _year.bindIncomeLabel(lblTotalIncome);
        _year.bindRemain(lblRestant);
        _year.calculateStatistic();
        tblActions.setDefaultRenderer(Object.class, new RendererAction());
    }

    @Override
    public String getTitle() {
        return ChocoMessages.message("Title_Resume");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toolbar = new javax.swing.JToolBar();
        mniAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnMonth_Statistic = new javax.swing.JButton();
        pnlBottom = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lblTotalIncome = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblTotalExpense = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblTotalDeficit = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblRestant = new javax.swing.JLabel();
        pnlCenter = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblActions = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnPrevious = new javax.swing.JButton();
        cmbMonths = new javax.swing.JComboBox();
        btnNext = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        toolbar.setFloatable(false);

        mniAdd.setText("Add");
        mniAdd.setFocusable(false);
        mniAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        mniAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        mniAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniAddActionPerformed(evt);
            }
        });
        toolbar.add(mniAdd);

        btnDelete.setText("Delete");
        btnDelete.setFocusable(false);
        btnDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        toolbar.add(btnDelete);

        btnMonth_Statistic.setText("Show month statistic");
        btnMonth_Statistic.setFocusable(false);
        btnMonth_Statistic.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnMonth_Statistic.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnMonth_Statistic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMonth_StatisticActionPerformed(evt);
            }
        });
        toolbar.add(btnMonth_Statistic);

        add(toolbar, java.awt.BorderLayout.NORTH);

        pnlBottom.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 20, 5));

        jLabel1.setText("Incomes :");
        pnlBottom.add(jLabel1);

        lblTotalIncome.setForeground(new java.awt.Color(0, 0, 255));
        lblTotalIncome.setText("jLabel1");
        pnlBottom.add(lblTotalIncome);

        jLabel2.setText("Expenses :");
        pnlBottom.add(jLabel2);

        lblTotalExpense.setForeground(new java.awt.Color(255, 0, 0));
        lblTotalExpense.setText("jLabel2");
        pnlBottom.add(lblTotalExpense);

        jLabel3.setText("Deficit :");
        pnlBottom.add(jLabel3);

        lblTotalDeficit.setText("jLabel3");
        pnlBottom.add(lblTotalDeficit);

        jLabel4.setText("Remaining :");
        pnlBottom.add(jLabel4);

        lblRestant.setText("jLabel1");
        pnlBottom.add(lblRestant);

        add(pnlBottom, java.awt.BorderLayout.SOUTH);

        pnlCenter.setLayout(new java.awt.BorderLayout());

        tblActions.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblActions);

        pnlCenter.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        btnPrevious.setText("<");
        jPanel1.add(btnPrevious);

        cmbMonths.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbMonths.setPreferredSize(new java.awt.Dimension(150, 20));
        jPanel1.add(cmbMonths);

        btnNext.setText(">");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        jPanel1.add(btnNext);

        pnlCenter.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        add(pnlCenter, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        _year.deleteSelectedIndex(tblActions);
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void mniAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniAddActionPerformed
        //Action newAction = new Action(_year.getSelectedMonthRef());
        DlgAddAction dlg = new DlgAddAction(null, true);
        dlg.setVisible(true);
        if (dlg.isConfirmed()) {
            dlg.addNewActionTo(_year); 
//            Calendar actionDate = dlg.getActionDate();
//            if (actionDate != null) {
//                Month ref = _year.getMonthRef(actionDate.get(Calendar.MONTH));
//                Action newAction = new Action(ref);
//                newAction.setDate(actionDate);
//                try {
//                    newAction.setDescription(dlg.getActionDescription());
//                    newAction.setAmount(dlg.getActionAmount());
//                    newAction.setCategorie(dlg.getActionCategorie());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
        }
    }//GEN-LAST:event_mniAddActionPerformed

    private void btnMonth_StatisticActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMonth_StatisticActionPerformed
        _parentFrame.showMonthStatitic();
    }//GEN-LAST:event_btnMonth_StatisticActionPerformed

    public class RendererAction extends DefaultTableCellRenderer {

        public RendererAction() {
            super();
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel laLabel = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (column == 3) {
                laLabel.setFont(new Font(null, Font.BOLD, 12));
                double doubleValue = (Double) value;
                String doubleString = Double.toString(doubleValue);
                if (doubleValue < 0) {
                    doubleString = "(" + doubleString + ")";
                    laLabel.setForeground(Color.red);
                } else {
                    laLabel.setForeground(Color.blue);
                }
                laLabel.setText(doubleString);
                if (row % 2 == 0){
                    laLabel.setBackground(Color.white);
                }else{
                    laLabel.setBackground(new Color(232,231,213));
                }
                
            } else if (column == 0) {
                laLabel.setFont(new Font(null, Font.BOLD, 12));
                if (value instanceof Categorie) {
                    Categorie c = (Categorie) value;
                    laLabel.setForeground(Color.white);
                    laLabel.setBackground(c.getColor());
                } else {
                    laLabel.setForeground(Color.black);
                    laLabel.setBackground(Color.white);
                }

            } else {
                laLabel.setForeground(Color.black);
                if (row % 2 == 0){
                    laLabel.setBackground(Color.white);
                }else{
                    laLabel.setBackground(new Color(232,231,213));
                }
            }
            if (isSelected) {
                laLabel.setBackground(laLabel.getBackground().darker());
            }
            return laLabel;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnMonth_Statistic;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JComboBox cmbMonths;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblRestant;
    private javax.swing.JLabel lblTotalDeficit;
    private javax.swing.JLabel lblTotalExpense;
    private javax.swing.JLabel lblTotalIncome;
    private javax.swing.JButton mniAdd;
    private javax.swing.JPanel pnlBottom;
    private javax.swing.JPanel pnlCenter;
    private javax.swing.JTable tblActions;
    private javax.swing.JToolBar toolbar;
    // End of variables declaration//GEN-END:variables
}
